package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.Constants;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.StringUtil;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.wechat.Wechat;

/**
 * Created by TCL on 2019/4/28.
 * Describe:
 */
public class JGShareActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_show)
    TextView tvShow;

    private ShareParams shareParams;
    private int shareType=-1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jg_share;
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_jg_share));
        toolbar.inflateMenu(R.menu.menu_jg_share_type);

        tvShow.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                tvShow.setText("");
                return true;
            }
        });
    }

    @Override
    protected void setListener() {
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.toolbar_0:
                        setShareType(0);
                        break;
                    case R.id.toolbar_1:
                        setShareType(1);
                        break;
                    case R.id.toolbar_2:
                        setShareType(2);
                        break;
                    case R.id.toolbar_3:
                        setShareType(3);
                        break;
                    case R.id.toolbar_4:
                        setShareType(4);
                        break;
                    case R.id.toolbar_5:
                        setShareType(5);
                        break;
                    case R.id.toolbar_6:
                        setShareType(6);
                        break;
                    case R.id.toolbar_7:
                        setShareType(7);
                        break;
                    case R.id.toolbar_8:
                        setShareType(8);
                        break;
                }
                return true;
            }
        });
    }

    private void setShareType(int i) {
        shareType = i;
        shareParams = new ShareParams();
        switch(i){
            case 0:
                shareParams.setTitle(Constants.share_title);
                shareParams.setText(Constants.share_text);
                shareParams.setShareType(Platform.SHARE_TEXT);
                break;

            case 1:
                shareParams.setUrl(Constants.share_url);
                shareParams.setShareType(Platform.SHARE_IMAGE);
                //twitter支持单张、多张（最多4张本地图片）
//                if(Twitter.Name.equals(platName)){
//                    String[] array = new String[]{ MyApplication.ImagePath, MyApplication.ImagePath};
//                    shareParams.setImageArray(array);
//                    //shareParams.setImagePath(MyApplication.ImagePath);
//                }else{
                    shareParams.setImagePath("");
//                }
                break;

            case 2:
                shareParams.setShareType(Platform.SHARE_IMAGE);
                //QQ空间支持多张图片，超出9张后，会变成上传相册，上传相册时只支持本地图片
//                if(platName.equals(QZone.Name)){
//                    String[] array = new String[]{ share_imageurl, share_imageurl_1};
//                    shareParams.setImageArray(array);
//                }else{
                    shareParams.setImageUrl(Constants.share_imageurl);
//                }
                break;
            case 3:
//                if(JChatPro.Name.equalsIgnoreCase(platName)){
//                    shareParams.setShareType(Platform.SHARE_IMAGETEXT);
//                    shareParams.setTitle(share_title);
//                    shareParams.setText(share_text);
//                }else {
                    shareParams.setShareType(Platform.SHARE_WEBPAGE);
                    shareParams.setTitle(Constants.share_title);
                    shareParams.setText(Constants.share_text);
                    shareParams.setUrl(Constants.share_url);
                    shareParams.setImageUrl(Constants.share_imageurl);
//                }
                break;
            case 4:
                shareParams.setTitle(Constants.share_title);
                shareParams.setText(Constants.share_text);
                shareParams.setShareType(Platform.SHARE_MUSIC);
//                if(platName.equals(SinaWeibo.Name)){
//                    shareParams.setUrl(Constants.share_musicurl);
//                }else{
                    shareParams.setMusicUrl(Constants.share_musicurl);
                    shareParams.setUrl(Constants.music_shareUrl);
//                    shareParams.setImagePath("");
//                }
                break;
            case 5:
//QQ空间支持本地视频
                shareParams.setShareType(Platform.SHARE_VIDEO);
//                if(platName.equals(QZone.Name) || platName.equals(Facebook.Name) || platName.equals(FbMessenger.Name) || platName.equals(Twitter.Name)){
//                    shareParams.setVideoPath(MyApplication.VideoPath);
//                }else{
                    shareParams.setTitle(Constants.share_title);
                    shareParams.setText(Constants.share_text);
                    shareParams.setUrl(Constants.share_videourl);
//                    shareParams.setImagePath("");
//                }
                break;
            case 6:
                //只有微信支持表情
                shareParams.setShareType(Platform.SHARE_EMOJI);
                shareParams.setImagePath("");
                shareParams.setFilePath("");
                break;
            case 7:
                //只有支持QQ APP
                shareParams.setShareType(Platform.SHARE_APPS);
                shareParams.setTitle(Constants.share_title);
                shareParams.setText(Constants.share_text);
                break;
            case 8:
                //只有微信支持文件
                shareParams.setShareType(Platform.SHARE_FILE);
                shareParams.setFilePath("");
                break;

            default:
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImageUrl(Constants.share_imageurl);
                break;
        }
    }

    @OnClick({R.id.btn_qq, R.id.btn_wechat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_qq:
                shareWithName(QQ.Name);
                break;
            case R.id.btn_wechat:
                shareWithName(Wechat.Name);
                break;
        }
    }

    private void shareWithName(String name) {
        if(StringUtil.checkIsNotNull(name)){
            if(shareParams!=null){
                if((QQ.Name).equals(name) && (shareType==-1||shareType==0||shareType==2||shareType==5||shareType==6||shareType==8)){
                    toastUnMatch(name,shareType);
                }else if((Wechat.Name).equals(shareType==7)){
                    toastUnMatch(name,shareType);
                }else{
                    JShareInterface.share(name, shareParams, mPlatActionListener);
                }
            }else{
                MyToast.toastError(this,"请选择分享方式",false);
            }
        }else{
            MyToast.toastError(this,"分享平台名称有误",false);
        }
    }

    private void toastUnMatch(String name, int shareType) {
        String type;
        switch(shareType){
            case 0:
                type = "文本";
                break;
            case 1:
                type = "图片";
                break;
            case 2:
                type = "纯图片url";
                break;
            case 3:
                type = "链接";
                break;
            case 4:
                type = "音乐";
                break;
            case 5:
                type = "视频";
                break;
            case 6:
                type = "emohi表情";
                break;
            case 7:
                type = "app分享";
                break;
            case 8:
                type = "文件分享";
                break;
            default:
                type = "默认";
                break;
        }
        MyToast.toastError(JGShareActivity.this, name + "不支持" + type + "分享",false);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String toastMsg = (String) msg.obj;
            tvShow.setText(toastMsg);
        }
    };

    private PlatActionListener mPlatActionListener = new PlatActionListener() {
        @Override
        public void onComplete(Platform platform, int action, HashMap<String, Object> data) {
            if(handler != null) {
                Message message = handler.obtainMessage();
                message.obj = "分享成功";
                handler.sendMessage(message);
            }
        }

        @Override
        public void onError(Platform platform, int action, int errorCode, Throwable error) {
            if(handler != null) {
                Message message = handler.obtainMessage();
                message.obj = "分享失败:" + (error != null ? error.getMessage() : "") + "---" + errorCode;
                handler.sendMessage(message);
            }
        }

        @Override
        public void onCancel(Platform platform, int action) {
            if(handler != null) {
                Message message = handler.obtainMessage();
                message.obj = "分享取消";
                handler.sendMessage(message);
            }
        }
    };
}
